﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using WebApi.Commons.Helpers;
using WebApi.Models.Common;

namespace WebApi.Controllers
{
    public class BaseController : Controller
    {
        protected UserInfoModel userAccount = new UserInfoModel();


        private string _token = string.Empty;

        protected string Token
        {
            get
            {
                return _token;
            }
        }

        /// <summary>
        /// 执行时操作
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var token = context.HttpContext.Request.Headers["authorization"];
            userAccount = JWTHelper.JWTDecode(token) ?? new UserInfoModel();
            if (!string.IsNullOrEmpty(token.ToString()))
            {
                _token = token.ToString().Split(" ")[1];
            }
        }

        /// <summary>
        /// 获取账号id
        /// </summary>
        protected long AccountId => userAccount?.Id ?? 0;

        /// <summary>
        /// 获取账号
        /// </summary>
        protected string Account => userAccount.Account ?? string.Empty;

        /// <summary>
        /// 获取账号姓名
        /// </summary>
        protected string UserName => userAccount.Name ?? string.Empty;
    }
}
